import java.util.concurrent.CompletableFuture;

public class Lesson14 {

    /**
     *  异步使用BiFunction处理两个阶段的结果
     *  类似上面的例子，但是有一点不同：依赖的前两个阶段异步地执行，所以thenCombine()也异步地执行，即时它没有Async后缀。
     *  所以我们需要join方法等待结果的完成。
     *
     *  Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method
     * */
    public static void main(String[] args) {
        String message = "Message";
        CompletableFuture completableFuture = CompletableFuture
                .completedFuture(message)
                .thenApplyAsync(s -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return s.toUpperCase();
                })
                .thenCombine(CompletableFuture.completedFuture(message).thenApplyAsync(String::toLowerCase), (s1, s2) -> s1 + s2)
                .thenCombine(CompletableFuture.completedFuture(message).thenApplyAsync(String::toLowerCase), (s1, s2) -> s1 + s2);
        completableFuture.join();
        System.out.println(completableFuture.getNow(null));
    }
}
